package model;

import java.io.Serializable;
import java.util.HashMap;

/**
 * The entity of track
 */
public class Track extends BaseModelItem implements ModelItem, Serializable {

    private Artist artist;
    private Album album;
    private Genre genre;
    private int length;
    private int size;

    public Track() {
    }

    /**
     * Constructor for the File System
     *
     * @param id identificator of the track
     * @param name name of the track
     * @param artist artist of the track
     * @param album album of the track
     * @param genre genre of the track
     * @param length length of the track in seconds
     * @param size size of the track in bytes
     */
    public Track(int id, String name, Artist artist, Album album, Genre genre, int length,
            int size) {
        super(id, name);
        this.artist = artist;
        this.album = album;
        this.genre = genre;
        this.length = length;
        this.size = size;
    }

    /**
     * Constructor for the View
     *
     * @param name name of the track
     * @param artist artist of the track
     * @param album album of the track
     * @param genre genre of the track
     * @param length length of the track in seconds
     * @param size size of the track in bytes
     */
    public Track(String name, Artist artist, Album album, Genre genre, int length,
            int size) {
        super(name);
        this.artist = artist;
        this.album = album;
        this.genre = genre;
        this.length = length;
        this.size = size;
    }

    public Artist getArtist() {
        return artist;
    }

    public void setArtist(Artist artist) {
        this.artist = artist;
    }

    public Album getAlbum() {
        return album;
    }

    public void setAlbum(Album album) {
        this.album = album;
    }

    public Genre getGenre() {
        return genre;
    }

    public void setGenre(Genre genre) {
        this.genre = genre;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public String toString() {
        StringBuffer str = new StringBuffer();
        str.append(getName());
        str.append("\t");
        str.append(artist);
        str.append("\t");
        str.append(album);
        str.append("\t");
        str.append(genre);
        str.append("\t");
        str.append(length);
        str.append("\t");
        str.append(size);
        return str.toString();
    }

    public boolean equals(Object obj) {
        return ((obj instanceof Track)
                && (((Track) obj).getName().equals(getName()))
                && (((Track) obj).getLength() == getLength())
                && (((Track) obj).getSize() == getSize())
                && (((Track) obj).getAlbum().equals(getAlbum()))
                && (((Track) obj).getArtist().equals(getArtist()))
                && (((Track) obj).getGenre().equals(getGenre()))
                && (((Track) obj).getVersion() == getVersion()));
    }

    public HashMap itemToMap() {
        HashMap map = new HashMap();
        HashMap innerMap = new HashMap();
        if(getName() != null) {
            innerMap.put("name", parseParametr(getName()));
        }  
        if (getAlbum().getName() != null) {
            innerMap.putAll(getAlbum().itemToMap());
        }
        if (getArtist().getName() != null) {
            innerMap.putAll(getArtist().itemToMap());
        }
        if (getGenre().getName() != null) {
            innerMap.putAll(getGenre().itemToMap());
        }
        if(getLength() != 0) {
            innerMap.put("length", getLength());
        }
        if(getSize() != 0) {
            innerMap.put("size", getSize());
        }
        map.put("track", innerMap);
        return map;
    }
}
